//
//  QuotationTopView.m
//  NekaPrice
//
//  Created by yu on 2018/6/16.
//  Copyright © 2018年 ma c. All rights reserved.
//  报价顶部视图

#import "QuotationTopView.h"
#import "ProductButton.h"
@implementation QuotationTopView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        //顶部logo
        UIImageView *logoImageView = [[UIImageView alloc] init];
        logoImageView.image = [UIImage imageNamed:@"logo"];
        logoImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:logoImageView];
        
        [logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(60);
            make.right.mas_equalTo(-60);
            make.bottom.mas_equalTo(-40);
        }];
        
        //小logo
        UIImageView *smallMarkView = [[UIImageView alloc] init];
        smallMarkView.contentMode = UIViewContentModeScaleToFill;
        smallMarkView.image = [UIImage imageNamed:@"neka"];
        [self addSubview:smallMarkView];
        
        //按钮下边的线
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [UIColor colorWithHexString:@"#5785ff"];
        [self addSubview:line];
        
        _btnArray = [[NSMutableArray alloc] init];
        NSArray *category = @[@"漆面保护膜", @"个性改色膜", @"安全隔热膜"];
        for (int i = 0; i < 3; i++) {
            ProductButton *btn = [[ProductButton alloc] init];
            [btn setTitle:category[i] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(selectProject:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = 10 + i;
            [_btnArray addObject:btn];
            [self addSubview:btn];
            if (i == 0) {
                [btn setSelected:YES];
            }
        }
        
        [smallMarkView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.bottom.mas_equalTo(-20);
            make.width.mas_equalTo(self.productLeadSpacing);
            make.height.mas_equalTo(self.productLeadSpacing * 82/233);
        }];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(smallMarkView.mas_right).offset(5);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(1);
            make.bottom.mas_equalTo(-20);
        }];
        
        [_btnArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:5 leadSpacing:self.productLeadSpacing + 5 tailSpacing:5];
        [_btnArray mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(30);
            make.bottom.mas_equalTo(-22);
        }];
    }
    return self;
}


- (void)selectProject:(ProductButton *)btn {
    for (UIButton *btn in _btnArray) {
        [btn setSelected:NO];
        btn.userInteractionEnabled = YES;
    }
    [btn setSelected:YES];
    //避免重复点击
    btn.userInteractionEnabled = NO;
    NSInteger index = btn.tag - 10;
    NSLog(@"%ld", index);
    if (_finishedBlock) {
        _finishedBlock(index);
    }
}


#pragma mark -- 尺寸相关
- (CGFloat)productButtonMinWidth {
    NSString *str = @"漆面保护膜";
    UIFont *font = [UIFont systemFontOfSize:12];
    CGFloat width = [str widthWithFixedHeight:30 andEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 5) attributes:@{NSFontAttributeName: font}];
    return width;
}

- (CGFloat)productItemSpacing {
    return 5;
}

- (CGFloat)productLeadSpacing {
    CGFloat leadSpacing = K_WIDTH - (self.productButtonMinWidth + self.productItemSpacing) * 3;
    return leadSpacing > 100 ? 100 : leadSpacing;
}


@end
